package xian.woniuxy.reghos.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.excel.utils.ExcelUtil;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import xian.woniuxy.reghos.domain.vo.DepVo;
import xian.woniuxy.reghos.domain.bo.DepBo;
import xian.woniuxy.reghos.service.IDepService;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;

import java.util.List;
import java.util.Arrays;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import javax.servlet.http.HttpServletResponse;

/**
 * 科室;控制器
 * 前端访问路由地址为:/reghos-serv/dep
 *
 * @author G
 * @date 2022-08-23
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/dep")
public class DepController extends BaseController {

    private final IDepService iDepService;

    /**
     * 查询科室;列表
     */
//    @SaCheckPermission("reghos-serv:dep:list")
    @GetMapping("/list")
    public TableDataInfo<DepVo> list(DepBo bo, PageQuery pageQuery) {
        return iDepService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出科室;列表
     */
//    @SaCheckPermission("reghos-serv:dep:export")
    @Log(title = "科室;", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(DepBo bo, HttpServletResponse response) {
        List<DepVo> list = iDepService.queryList(bo);
        ExcelUtil.exportExcel(list, "科室;", DepVo.class, response);
    }

    /**
     * 获取科室;详细信息
     *
     * @param depId 主键
     */
//    @SaCheckPermission("reghos-serv:dep:query")
    @GetMapping("/{depId}")
    public R<DepVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long depId) {
        return R.ok(iDepService.queryById(depId));
    }

    /**
     * 新增科室;
     */
//    @SaCheckPermission("reghos-serv:dep:add")
    @Log(title = "科室;", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody DepBo bo) {
        return toAjax(iDepService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改科室;
     */
//    @SaCheckPermission("reghos-serv:dep:edit")
    @Log(title = "科室;", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody DepBo bo) {
        return toAjax(iDepService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除科室;
     *
     * @param depIds 主键串
     */
//    @SaCheckPermission("reghos-serv:dep:remove")
    @Log(title = "科室;", businessType = BusinessType.DELETE)
    @DeleteMapping("/{depIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] depIds) {
        return toAjax(iDepService.deleteWithValidByIds(Arrays.asList(depIds), true) ? 1 : 0);
    }
}
